package belastungstest.mobapp.fhfl.mobappbelastungstest;

import android.util.Log;

import java.nio.ByteBuffer;


/**
 * Created by Iwo on 05.06.2014.
 */
public class ContentFactory
{
    static final byte[] testArray = {0,1,0,1,0,1,0};
    /**
     * Größe der Datenmenge in Byte
     */
    private int packetSize;
    private int packetNumber = 0;
    private byte[] data;

    /**
     * Constructor which set the packetSize
     * @param packetSize Die Größe des Packets in Byte
     */
    public ContentFactory(int packetSize)
    {
        this.packetSize = packetSize;
        this.data = new byte[this.packetSize];
        createData();
    }

    private void createData()
    {
        for (int i = MagicNumbers.DATE_LENGTH + MagicNumbers.PACKET_NUMBER_LENGTH; i < this.packetSize; i++)
            this.data[i] = MagicNumbers.CONTENT;
    }

    public byte[] setPackage()
    {
        return  ByteBuffer.wrap(this.data,0,this.packetSize).putLong(System.currentTimeMillis()).putInt(packetNumber).array();
    }

    public long getTimeFromPacket( byte[] in )
    {
        return ByteBuffer.wrap(in, 0, MagicNumbers.DATE_LENGTH).getLong();
    }

    public int getPacketNumberFromPacket( byte[] in)
    {
        return ByteBuffer.wrap(in, MagicNumbers.DATE_LENGTH, MagicNumbers.PACKET_NUMBER_LENGTH).getInt();
    }

    public float getPacketPercentage( byte[] in )
    {
        int sum = 0;

        for(int i = MagicNumbers.DATE_LENGTH + MagicNumbers.PACKET_NUMBER_LENGTH; i < this.packetSize; i++){
            sum += (in[i]==this.data[i])?1:0;
        }

        return (100.0f * sum / this.getPacketSize());
    }

    /**
     * Gibt die Packetgröße in Byte zurück.
     * @return Die Packetgröße in Byte
     */
    public int getPacketSize()
    {
        return this.packetSize - (MagicNumbers.DATE_LENGTH + MagicNumbers.PACKET_NUMBER_LENGTH);
    }
    public void incrementPacketNumber()
    {
        if (packetNumber < Integer.MAX_VALUE)
            packetNumber++;
        else
            packetNumber = 0;
    }

    public int getSendSize()
    {
        return this.packetSize;
    }
}
